package com.sd.repay.task;

import com.sd.repay.common.CommonResult;
import com.sd.repay.pojo.ExtractionOrder;
import com.sd.repay.service.ExtractOrderService;
import com.sd.repay.service.ReverseRecordService;
import com.sd.repay.utils.JsonUtil;
import com.sd.repay.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 提现冲正定时任务
 * Created by sd on 2018/08/30.
 */
@Component
public class ExtractReverseTask {
    private static Logger log = LoggerFactory.getLogger(ExtractReverseTask.class);

    @Autowired
    private ExtractOrderService extractOrderService;
    @Autowired
    private ReverseRecordService reverseService;

    /**
     * 根据需要冲正，并且冲正未成功的提现记录，重新冲正
     */
    public void executeExtractReverse() {
        log.info("--------上游同步订单状态定时任务开始执行---------");
        try {
            List<ExtractionOrder> reverseExtractOrders = extractOrderService.getReverseExtractOrder();
            if (CollectionUtils.isEmpty(reverseExtractOrders)) {
                log.info("没有需要冲正的提现订单");
                return;
            }
            log.info("--------需要冲正的提现订单记录数-------------" + reverseExtractOrders.size());

            CommonResult reverseRes = null;
            String extOrderNo = "";

            for (ExtractionOrder extOrder : reverseExtractOrders) {

                extOrderNo = extOrder.getOrderNo();
                log.info("============冲正的订单=============[{}]", extOrderNo);
                reverseRes = reverseService.executeReverse(extOrder);
                log.info("============订单[{}]冲正返回[{}]=============", new Object[]{extOrderNo, JsonUtil.objectToJson(reverseRes)});
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("-----------提现冲正定时任务完成--------------");
        WebUtil.removeBlockTaskBySpringId(WebUtil.getClassSimpleName(this.getClass()));
    }

}
